Skip to content

ENH: seed sensor measurement noise per instance - #13

Merged
thc1006 merged 2 commits into
developfrom
fix/deterministic-sensor-noise
Jun 27, 2026
Merged

ENH: seed sensor measurement noise per instance#13
thc1006 merged 2 commits into
developfrom
fix/deterministic-sensor-noise

Conversation

@thc1006

@thc1006 thc1006 commented Jun 27, 2026

Copy link
Copy Markdown
Member

Sensor noise (white noise and random-walk drift in the inertial sensors, plus the GNSS position/velocity accuracy) was drawn from the global numpy RNG, so it couldn't be reproduced from a seed and was unsafe under multiprocess, where workers share the global state.

This routes every sensor's noise through a per-instance numpy Generator, via a new seed argument on the sensor constructors. seed=None keeps the random-by-default behaviour, but per instance, so it no longer touches the global RNG. Reproducibility lives at construction, since a freshly-seeded sensor replays the same sequence. That's the same approach the stochastic stack already uses (StochasticModel / MonteCarlo): seed once, draw sequentially.

I've also raised this upstream at RocketPy-Team#1042 so we can pull it back once it lands there.

Tests: 6 new in tests/unit/sensors/test_sensor_seeding.py (reproducible from a seed, decorrelated across sensors, independent of the global RNG, ideal/zero-noise unchanged) plus the existing 36 sensor unit tests pass.

@thc1006
thc1006 force-pushed the fix/deterministic-sensor-noise branch from c7ff674 to 18b9952 Compare June 27, 2026 17:58

@zuorenchen zuorenchen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intresting, I've never checked this part and noticed that the sensor noise generation is not seeded

@zuorenchen

Copy link
Copy Markdown
Member

Before merging, could you fix this lint error? thanks

image

@thc1006

thc1006 commented Jun 27, 2026

Copy link
Copy Markdown
Member Author

Before merging, could you fix this lint error? thanks

image

for sure

@zuorenchen

Copy link
Copy Markdown
Member

Issue opened in BPC to implement this. ARRC-Rocket/BalloonPoppingChallenge#52

Sensor noise (white noise and random-walk bias drift in InertialSensor
and ScalarSensor, plus the GNSS position/velocity accuracy) was drawn
from the global numpy RNG. That made it impossible to reproduce from a
seed, and unsafe under multiprocess where workers share the global state.

Add a seed argument to the sensor constructors and draw all noise from a
per-instance numpy Generator. seed=None keeps the random-by-default
behaviour but per instance, so it no longer touches the global RNG,
matching how StochasticModel and MonteCarlo already seed. A
freshly-constructed sensor replays the same sequence, so reproducibility
lives at construction rather than in _reset.

Adds tests/unit/sensors/test_sensor_seeding.py.
@thc1006
thc1006 force-pushed the fix/deterministic-sensor-noise branch from 18b9952 to 15daf41 Compare June 27, 2026 18:15
@thc1006 thc1006 self-assigned this Jun 27, 2026
test_noisy_barometer asserts a barometer reading within rel=0.03 of the clean
pressure, but the reading carries an unseeded Gaussian noise draw plus a
+1000 Pa constant_bias the expected value omits, leaving only ~+2.42 sigma of
headroom, so it already flaked at ~0.76% on develop (a 20k-trial check: develop
0.77%, this branch 0.75%). Passing a fixed seed makes it deterministic and also
exercises the new sensor seed argument.
@thc1006
thc1006 merged commit e9ebff6 into develop Jun 27, 2026
8 checks passed
@thc1006
thc1006 deleted the fix/deterministic-sensor-noise branch June 27, 2026 19:04
Comment thread tests/fixtures/sensors/sensors_fixtures.py
thc1006 added a commit that referenced this pull request Jun 27, 2026
Following @zuorenchen's review note on #13, seed the remaining noisy sensor
fixtures (noisy_rotated_accelerometer, noisy_rotated_gyroscope) the same way
noisy_barometer already is, so the whole sensor suite is deterministic instead
of relying on statistical tolerance bounds. Uses the seed argument added in #13.
zuorenchen added a commit that referenced this pull request Jul 26, 2026
* Add actuator dynamics in TVC and ThrottleControl

* Refactor actuator_tau parameters to use None as default in TVC and ThrottleControl

* Add actuator dynamics to RollControl

* Move actuators to a new actuator folder and rename actuator classes

* Refactor roll, throttle, and thrust vector acutator with a new actuator class

* Add pytest for all actuators

* Update rocket.py and flight.py for actuator class update

* Fix actuator class calls

* Update active control example with new actuator class

* Update time_overshoot in flight.py to align rocketpy commit #45a89

RocketPy-Team@45a891e

* Fix actuator unit tests

* Enhance ThrustVectorActuator2D with serialization methods and improve documentation

* Fix comment msg

* Fix pylint unused parameters

* Remove unused import

* Fix pylint warnings

* Refactor __run_in_serial() to fix pylint too much statement warning

* Fix: use warning instead of printf

* Fix comment

* Fix: use warning instead of printf

* Run ruff format

* Fix thrust3 and effective_thrust name

* Fix pytests

* Fix pytest utilities

* Fix integration/gnss pytest

* Update readme

* ENH: seed sensor measurement noise per instance (#13)

* ENH: seed sensor measurement noise per instance

Sensor noise (white noise and random-walk bias drift in InertialSensor
and ScalarSensor, plus the GNSS position/velocity accuracy) was drawn
from the global numpy RNG. That made it impossible to reproduce from a
seed, and unsafe under multiprocess where workers share the global state.

Add a seed argument to the sensor constructors and draw all noise from a
per-instance numpy Generator. seed=None keeps the random-by-default
behaviour but per instance, so it no longer touches the global RNG,
matching how StochasticModel and MonteCarlo already seed. A
freshly-constructed sensor replays the same sequence, so reproducibility
lives at construction rather than in _reset.

Adds tests/unit/sensors/test_sensor_seeding.py.

* TST: seed the noisy_barometer fixture to fix a pre-existing flake

test_noisy_barometer asserts a barometer reading within rel=0.03 of the clean
pressure, but the reading carries an unseeded Gaussian noise draw plus a
+1000 Pa constant_bias the expected value omits, leaving only ~+2.42 sigma of
headroom, so it already flaked at ~0.76% on develop (a 20k-trial check: develop
0.77%, this branch 0.75%). Passing a fixed seed makes it deterministic and also
exercises the new sensor seed argument.

* TST: seed the noisy accelerometer and gyroscope fixtures (#14)

Following @zuorenchen's review note on #13, seed the remaining noisy sensor
fixtures (noisy_rotated_accelerometer, noisy_rotated_gyroscope) the same way
noisy_barometer already is, so the whole sensor suite is deterministic instead
of relying on statistical tolerance bounds. Uses the seed argument added in #13.

* ENH: pull v1.13 from RocketPy (#16)

* test: characterization tests for Flight.step_simulation() (#11)

* test: characterization tests for Flight.step_simulation()

Verify the stepped-simulation API (run_simulation=False plus repeated
step_simulation() calls) reproduces a one-shot simulate():

- initial step state is unfinished at the first phase
- stepping visits multiple phases and reaches the finished state
- the stepped trajectory (final t and the full solution array) matches simulate()
  to a tight tolerance, robust to LSODA last-bit noise across platforms
- post_process_simulation / initialize_prints_plots fire on finish
  (t_final, prints, plots present)
- stepping after finished is a no-op

Uses the parachute-free calisto flight (parachute triggers are not migrated into
the stepping path); the twin's launch parameters are read back from the
reference so it cannot drift. Scope: uncontrolled stepping only.

* test: cover controlled stepping in step_simulation()

Inject a roll command between step_simulation() calls (the Balloon Popping
Challenge use case) and assert the trajectory responds:

- a sustained command spins the body up (roll rate w3 grows) while a neutral
  command leaves w3 at zero
- a mid-flight command reversal turns the roll rate around -- only possible if
  the command is re-read every step (a latched command could not)
- a zero command leaves the angular state bit-identical to an uncontrolled run,
  isolating the divergence as the command, not the actuator's presence

Uses time_overshoot=False (as BPC does) so each step advances one solver node
and the command is injected per timestep. Tolerances/thresholds are used rather
than bit-exact equality, to stay robust to LSODA last-bit noise across platforms.

---------

Co-authored-by: RickyRicato <rickywang44@gmail.com>
Co-authored-by: ChiChun Wang <99960850+chichunwang@users.noreply.github.com>
Co-authored-by: 秀吉 <84045975+thc1006@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants